<?php


namespace app\index\controller;
use app\common\server\CartServer;

class Cart extends Base
{
    /**
     * 添加购物车
     */
    public function add()
    {
        $data =input('post.');
        $data['id'] = $this->user['id'];
        $add_cart =new CartServer();
        $add_cart->addCart($data);
        return $add_cart;
    }

    /**
     * 购物车列表
     */
    public function index()
    {
        $map['us_id'] = $this->user['id'];
        $info = db('cart')->where($map)->select();
        foreach ($info as $key => $value) {
            // 产品库存
            $inventory = model('Product')->where('id',$value['pd_id'])->value('pd_inventory');
            $info[$key]['inventory'] = $inventory;
        }
        $this->s_msg(null,$info);
    }

    /**
     * 购物车数量修改
     */
    public function changeNum()
    {
        $id = input('post.cart_id');
        if (input('post.goods_num') <= 0 || floor(input('post.goods_num'))-input('post.goods_num') != 0) {
            $this->e_msg('输入参数错误');
        }
        $data['goods_num'] = input('post.goods_num')?input('post.goods_num'):1;
        $rel = model('cart')->where('id',$id)->update($data);
        if($rel){
            $this->s_msg('成功');
        }
        $this->e_msg('失败');
    }

    /**
     * 删除购物车商品
     */
    public function deleteCart()
    {
        $id = input('post.cart_id');
        $rel = model('cart')->where('id',$id)->delete();
        if($rel){
            $this->s_msg('成功');
        }
        $this->e_msg('失败');
    }



    /**
     * 购物车列表结算生成订单
     */
    public function addOrder()
    {
        $us_id =$this->user['id'];
        $goods_num = input('post.goods_num');         //商品个数
        $goods_id = input('post.goods_id');                 //商品id
        $goods_norm = input('post.goods_norm');            //商品规格
        $map['us_id'] = $us_id;
        $map['addr_default'] = 1;
        $addr_id = model('UserAddr')->where($map)->value('id');
        if(!$addr_id){
            $this->e_msg('请填写收货地址');
        }
        $ids = input('post.id');
        if(!$ids){
            $this->e_msg('请选择商品');
        }
        $order = [
            'us_id' => $us_id,        //用户id
            'or_status' => 0,          //订单状态
            'addr_id' => $addr_id,    //收货地址id
            'or_add_time' => date('Y-m-d H:i:s'), //添加订单时间
            'or_num' => date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT)  //订单号
        ];
        $order_id = model('order')->addInfo($order);
        if (!$order_id) {
            $this->result('', '0', '订单添加失败', 'json');
        }
        $goods = model('Product')->where('id', $goods_id)->find();
        //订单详情
        $order_detail = [
            'pd_spec' => $goods_norm,         //商品规格
            'or_id' => $order_id,            //订单id
            'ca_id' => $goods['ca_id'],       //商品分类id
            'pd_id' => $goods['id'],           //产品id
            'or_pd_name' => $goods['pd_name'],  //产品名称
            'or_pd_pic' => $goods['pd_pic'],    //产品图
            'or_pd_price' => $goods['pd_now_price'],          //产品单价
            'or_pd_num' => $goods_num,                        //产品数量
//            'sku_id' => $goods_norm_id,                      //商品规格id
            'or_pd_content' => $goods['pd_content'],            //商品描述
        ];
        $rel = model('OrderDetail')->addInfo($order_detail);
        if (!$rel) {
            $this->error('详情添加失败');
        }
        $order_id = encrypt($order_id);
        $this->result($order_id, '1', '订单提交成功', 'json');
    }

}